/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frm.billingAndPaymentProcessing.managedbean;

import crm.salesOrderProcessing.entity.LineItemEntity;
import frm.billingAndPaymentProcessing.entity.IncomingBillEntity;
import frm.billingAndPaymentProcessing.session.IncomingBillSessionBean;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import scm.rawMaterialPurchasing.entity.RawMaterialPurchasingOrderEntity;
import scm.rawMaterialPurchasing.entity.SupplierEntity;
import scm.rawMaterialPurchasing.session.RawMaterialPurchasingSessionBean;
import scm.rawMaterialPurchasing.session.SupplierSessionBean;

/**
 *
 * @author xhx
 */
@ManagedBean (name="createIncomingBillRecordManagedBean")
@ViewScoped
public class CreateIncomingBillRecordManagedBean {
private IncomingBillEntity incomingBillEntity;
private Long purchasingOrderId;
@EJB
IncomingBillSessionBean incomingBillSessionBean;
@EJB
RawMaterialPurchasingSessionBean rawMaterialPurchasingSessionBean;
@EJB
SupplierSessionBean supplierSessionBean;
    /** Creates a new instance of CreateIncomingBillRecordManagedBean */
    public CreateIncomingBillRecordManagedBean() {
        incomingBillEntity = new IncomingBillEntity();
    }
    public void createIncomingBill() throws IOException{
        if(!incomingBillSessionBean.containBillId(incomingBillEntity.getBillId())){
            try{
           RawMaterialPurchasingOrderEntity rawMaterialPurchasingOrderEntity = rawMaterialPurchasingSessionBean.getRawMaterialPurchasingOrderById(purchasingOrderId);  
            incomingBillEntity.setRawMaterialPurchasingOrder(rawMaterialPurchasingOrderEntity);
            }
            catch(EJBException ex){
               FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "PurchasingOrder is not Found", ""));  
               return;
            }
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long employeeId = (Long) request.getSession().getAttribute("userId");
        incomingBillEntity.setAmountPayable(incomingBillEntity.getTotalDue());
        incomingBillEntity.setCreatedBy(employeeId);
        incomingBillEntity.setCreateDate(Calendar.getInstance().getTime());
        incomingBillSessionBean.createIncomingBill(incomingBillEntity);
       
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("message", "IncomingBillCreation");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("incomingBillEntity", incomingBillEntity);
        FacesContext.getCurrentInstance().getExternalContext().redirect("displayIncomingBill.xhtml");
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Incoming Bill Record Has Already Been Created", ""));
    }
        public List<SelectItem> getSupplierList() { //This is the method that returns a dynimic list of proudcts available in the quotation
        List<SupplierEntity> supplierList = supplierSessionBean.getAllSuppliers();
        if (supplierList.isEmpty()) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "The Supplier List is Blank", ""));
            return null;
        }
        List<SelectItem> selectItems = new ArrayList<SelectItem>();


        for (SupplierEntity supplier : supplierList) {

            selectItems.add(new SelectItem(supplier.getSupplierName()));
        }
        return selectItems;
    }
      public List<SelectItem> getAllPurchasingOrderId() { //This is the method that returns a dynimic list of proudcts available in the quotation
        List<RawMaterialPurchasingOrderEntity> rmPurchaseOrderList = rawMaterialPurchasingSessionBean.getAllRawMaterialPurchasingOrder();
        if (rmPurchaseOrderList.isEmpty()) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "The Supplier List is Blank", ""));
            return null;
        }
        List<SelectItem> selectItems = new ArrayList<SelectItem>();


        for (RawMaterialPurchasingOrderEntity rmPurchaseOrder : rmPurchaseOrderList) {

            selectItems.add(new SelectItem(rmPurchaseOrder.getId()));
        }
        return selectItems;
    }
    public IncomingBillEntity getIncomingBillEntity() {
        return incomingBillEntity;
    }

    public void setIncomingBillEntity(IncomingBillEntity incomingBillEntity) {
        this.incomingBillEntity = incomingBillEntity;
    }

    public Long getPurchasingOrderId() {
        return purchasingOrderId;
    }

    public void setPurchasingOrderId(Long purchasingOrderId) {
        this.purchasingOrderId = purchasingOrderId;
    }
}
